<?php
namespace app\wechat\controller;
use think\Controller;
use SimpleXMLElement;
class Special extends Controller 
{
	protected $totalCounts=[
			'A'=>'严重违法',
			'B'=>'逾期风险',
			'C'=>'诉讼风险',
			'D'=>'潜在风险',
			'E'=>'多头贷款',
	];
	protected $list=[
			'A'=>'严重违法',
			'A02'=>'名下公司税务重大违法',
			'B'=>'逾期风险',
			'B01'=>'失联',
			'B02'=>'贷款不良',
			'B03'=>'短时逾期',
			'B04'=>'逾期',
			'C'=>'诉讼风险',
			'C01'=>'失信被执行人',
			'C02'=>'被执行人',
			'C03'=>'裁判文书',
			'D'=>'潜在风险',
			'D01'=>'疑似催收风险',
			'D02'=>'名下公司存在违规行为',
			'D03'=>'来自信贷高风险区域 ',
			'D04'=>'其他潜在风险',
			'E'=>'多头借贷',
			'E01'=>'7天内贷款数',
			'E02'=>'一个月内贷款数',
			'E03'=>'三个月内贷款数',
	];
    
    /**
    * 处理支付的反馈结果——积分兑换支付	2017-01-05
    */
    public function integral(){
    	$uid = input('get.uid');
    	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
    	$data = xmltoarray($xml);
	
    	$pay = db(\tname::weixin_pay) -> where('uid',$uid) -> find();
    	$wxpayparam = serializeMysql($pay['wxpay'],1);
    	$Wxpay= controller('purewechat/Wxpay');
    	$sign = $Wxpay -> checksign($data,$wxpayparam);
    	$returnParameters = array();
    	if($sign == false){
    		$returndata = array(
    			'return_code'	=> 'FAIL',
    			'return_msg'	=> '签名失败'
    		);   
    	}else{
    		$returndata = array(
    			'return_code'	=> 'SUCCESS',
    			'return_msg'	=> 'OK'
    		);
    	} 	
    	$xml = new SimpleXMLElement('<xml></xml>');
    	data2xml($xml, $returndata);
    	$returnXml = $xml->asXML();
    	echo $returnXml;
		
    	//以下为微信支付需要修改的
    	$orderinfo = db(\tname::integral_order) -> where('ordernumber',$data['out_trade_no']) -> find();
    	if($orderinfo['ispay'] != 1){
    		//记录微信支付返回结果
    		apilog($uid,'wechat','wxpaynotify-integral','', serializeMysql($data), serializeMysql($returndata));
    		
    		//修改订单数据
    		$orderdata=array(
    			'ispay' => 1,
    			'paytime'=>time(),
    			'status'=>1
    		);
    		$res = db(\tname::integral_order) -> where(array('ordernumber'=>$data['out_trade_no'])) -> update($orderdata);
    		
    		//积分变动   		
    		integrallog($uid,'integral',$orderinfo['openid'],$orderinfo['payintegral'],$orderinfo['id'],2);
    		
    	}
    }
    

    
    public function couponupdate($uid){
    	$where['uid'] = $uid;
    	$where['ispublish'] = 1;
    	$where['ishidden'] = 0;
    	$where['activityetime'] = array(array('gt',0),array('elt',time()));
    	db(\tname::coupon) -> where($where) -> update(array('ispublish'=>0));
    }
    
    public function mycouponupdate($uid){
    	$where['uid'] = $uid;
    	$where['status'] = 0;
    	$where['etime'] = array('elt',time());
    	db(\tname::coupon_mycoupon) -> where($where) -> update(array('status'=>-1));
    
    	if($this -> parameter['autodel']){
    		$autodel = $this -> parameter['autodel'];//设置几天自动过期
    		$where['uid'] = $uid;
    		$where['userhide'] = 0;
    		$where['status'] = array('in','-1,1');
    		$where['etime'] = array('elt',time() - $autodel * 86400);
    		db(\tname::coupon_mycoupon) -> where($where) -> update(array('userhide'=>1));
    	}
    }
	/**
	 * 处理支付的反馈结果——订单支付	2017-09-12
	 */
	public function order(){
		$uid = input('param.uid');

		$xml = file_get_contents('php://input');
		$data = xmltoarray($xml);
		$pay = db(\tname::weixin_pay)->where(array('uid' => $uid))->find();
		$wxpayparam = serializeMysql($pay['wxpay'], 1);
		$Wxpay = new \wechat\Wxpay();
		$sign = $Wxpay->checksign($data, $wxpayparam);
		$returnParameters = array();
		if ($sign == false) {
			$returndata = array(
					'return_code' => 'FAIL',
					'return_msg' => '签名失败'
			);
		} else {
			$returndata = array(
					'return_code' => 'SUCCESS',
					'return_msg' => 'OK'
			);
		}
		$xml = new SimpleXMLElement('<xml></xml>');
		data2xml($xml, $returndata);
		$returnXml = $xml->asXML();
		echo $returnXml;

		$orderinfo = db('shuju_order') -> where(array('ordernumber'=>$data['out_trade_no'])) -> find();
		if($orderinfo['ispay'] != 1){
			//修改订单数据
			$orderdata=array(
				'ispay'  => 1,
			);
			$res = db('shuju_order') -> where(array('ordernumber'=>$orderinfo['ordernumber'])) -> update($orderdata);
		}
	}

	/**
	 * 定时任务
	 */
	public function dingshi(){
		$data['free']=0;
		$data['is_free']=0;
		$vip=db(\tname::vip) -> where(array('uid'=>2)) -> update($data);
	}
}